from rest_framework.generics import GenericAPIView
# 五个拓展类，封装了五个业务逻辑
# CreateModelMixin 保存
# UpdateModelMixin 更新
# ListModelMixin 获取多个数据，
# DestroyModelMixin 删除
# RetrieveModelMixin 获取单一数据
# 五个拓展类中使用了GenericAPIView提供的get_serialzier()、get_queryset()、get_object（）
from rest_framework.mixins import CreateModelMixin, UpdateModelMixin, ListModelMixin, DestroyModelMixin, RetrieveModelMixin

from book.modelserializer import BookModelSerialzier
from book.models import BookInfo


class BooksView(GenericAPIView, CreateModelMixin, ListModelMixin):
    # GenericAPIView和APIView使用区别 指定两个属性
    # 序列化器属性
    serializer_class = BookModelSerialzier
    # 查询集属性
    queryset = BookInfo.objects.all()

    def post(self, request):
        # create()是CreateModelMixin拓展类中封装保存业务逻辑
        return self.create(request)

    def get(self, request):
        """
            获取所有图书
        :param request:
        :return:
        """
        # list()是ListModelMixin拓展类中封装获取多个数据的业务逻辑
        return self.list(request)


class BookView(GenericAPIView, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin):
    # 指定序列化器属性
    serializer_class = BookModelSerialzier
    # 指定查询集属性
    queryset = BookInfo.objects.all()

    def get(self, request, pk):
        """
            获取单一图书
        :param request:
        :return:
        """
        # retrieve()方法是RetrieveModelMixin封装获取单一数据的方法
        return self.retrieve(request)

    def put(self, request, pk):
        """
            更新图书
        :param request:
        :return:
        """
        # update()方法是UpdateModelMixin封装更新数据的方法
        return self.update(request)

    def delete(self, request, pk):
        """
              删除图书
          :param request:
          :return:
         """
        # destroy()方法是DestroyModelMixin封装删除数据的方法
        return self.destroy(request)
